package hch.redesFerroviarias;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServidorTrasbordo {
	public static void main(String[] args) {
		ServerSocket serverSocketTrasbordo;
		try {
			serverSocketTrasbordo = new ServerSocket(1234);
			while(true){
				Socket serverSoc1 = serverSocketTrasbordo.accept();
				Socket serverSoc2 = serverSocketTrasbordo.accept();
	
				BufferedReader serverIn1 = new BufferedReader(new InputStreamReader(serverSoc1.getInputStream()));
				PrintStream serverOut1 = new PrintStream(serverSoc1.getOutputStream());
				BufferedReader serverIn2 = new BufferedReader(new InputStreamReader(serverSoc2.getInputStream()));
				PrintStream serverOut2 = new PrintStream(serverSoc2.getOutputStream());
				new ThreadPasamanos(serverOut1,serverIn2).start();
				new ThreadPasamanos(serverOut2,serverIn1).start();
			}
		}
		catch (IOException e) {
			System.out.println("Could not listen on port: 1234");
			System.exit(-1);
			}
		}
}
